package com.ddone.demo.controller;

import com.ddone.demo.bean.ValidUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author ddone
 * @date 2024/3/23-18:13
 */
@Controller
@RequestMapping("/valid")
@Slf4j
public class ValidController {

    @PostMapping("/user")
    public String saveUser(@Validated ValidUser user, BindingResult bindingResult){
        log.warn("user = " + user);
        if(bindingResult.hasErrors()){
            // 获取错误的描述
            bindingResult.getFieldErrors().forEach(System.out::println);
            // 如果有绑定错误,返回结果为true
            // 跳转展示页面, 混合开发创建的提示
            // 如果有错误,他会将错误放到共享域对象 key是对应绑定实体的形参变量名
            return "index";
        }
        return "index";
    }

}
